//解密
#include <iostream>
#include <string>
#include <cryptlib.h>
#include <aes.h>
#include <modes.h>
#include <filters.h>
#include <base64.h>
using namespace CryptoPP;
using namespace std;
string fax(const string& encryptedMessage, const SecByteBlock& key) 
{
    try 
    {
        string decoded, iv, ciphertext;
        StringSource(encryptedMessage, true,
            new Base64Decoder(new StringSink(decoded)));
        iv = decoded.substr(0, AES::BLOCKSIZE);
        ciphertext = decoded.substr(AES::BLOCKSIZE);
        CBC_Mode<AES>::Decryption decryptor;
        decryptor.SetKeyWithIV(key, key.size(), (const byte*)iv.data());
        string plaintext;
        StringSource(ciphertext, true,
            new StreamTransformationFilter(decryptor, new StringSink(plaintext)));
        return plaintext;
    }
    catch (const Exception& e)
      {
        cerr << "Decryption failed: " << e.what() << endl;
        exit(1);
    }
}
int main() 
{
    AutoSeededRandomPool rng;
    SecByteBlock key(AES::DEFAULT_KEYLENGTH);
    rng.GenerateBlock(key, key.size());
    string plaintext = "这是一个加密的安全消息！";
    SecByteBlock iv(AES::BLOCKSIZE);
    string encryptedMessage = nax(plaintext, key, iv);
    string decryptedMessage = fax(encryptedMessage, key);
    cout << "Original Message: " << plaintext << endl;
    cout << "Encrypted Message (Base64): " << encryptedMessage << endl;
    cout << "Decrypted Message: " << decryptedMessage << endl;
    return 0;
}
